/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

import dao.ProblemJpaController;
import entities.KeyWord;
import entities.Problem;
import entities.ProblemStats;
import java.util.List;
import vo.VOProblem;

public class ProblemService {
    private static ProblemJpaController pc;
    private static ProblemStatsService pss;
    private static KeyWordService ks;

    private static ProblemJpaController getJPAController()
    {
        if(pc == null)
            pc = new ProblemJpaController();
        return pc;
    }


    private ProblemStatsService getStatsService()
    {
        if(pss == null)
            pss = new ProblemStatsService();
        return pss;
    }
    
    public boolean saveProblem(VOProblem vo)
    {
        boolean ret = true;
        pc = getJPAController();
        try
        {
            pc.create(new Problem(0,vo.getName(), vo.getDescription(), vo.getDifficulty(), vo.getTime()));
        }
        catch (Exception e)
        {
            ret = false;
        }
        return ret;
    }

    public Problem getProblem(String name)
    {
        pc = getJPAController();
        return pc.getProblemByName(name);
    }

    public boolean editProblem(Problem p)
    {
        pc = getJPAController();
        try
        {
            pc.edit(p);
            return true;
        } catch (Exception e)
        {
            return false;
        }
    }

    public boolean deleteProblem(Problem p)
    {
        pc = getJPAController();
        pss = getStatsService();
        try
        {
            pss.deleteProblemStats(pss.getStatsByProblem(p));
            pc.destroy(p.getIdProblem());
            return true;
        }
        catch (Exception e)
        {
            System.out.println("Exception at ProblemService.deleteProblem: " + e.getMessage());
            return false;
        }
    }

    public Problem getProblem(int id)
    {
        pc = getJPAController();
        return pc.findProblem(id);
    }

    public boolean addKeyword(Problem p, KeyWord k)
    {
        p.getKeyWordCollection().add(k);
        k.getProblemCollection().add(p);
        pc = getJPAController();
        ks = getKeyWordService();
        try
        {
            pc.edit(p);
        } catch (Exception e)
        {
            return false;
        }
        return ks.editKeyWord(k);
    }
    private KeyWordService getKeyWordService()
    {
        if (ks == null)
        {
            ks = new KeyWordService();
        }
        return ks;
    }

    public List<Problem> getAllProblems()
    {
        pc = getJPAController();
        return pc.findAll();
    }

    public boolean removeKeyWord(Problem p, KeyWord k)
    {
        p.getKeyWordCollection().remove(k);
        k.getProblemCollection().remove(p);
        pc = getJPAController();
        ks = getKeyWordService();
        try
        {
            pc.edit(p);
        } catch (Exception e)
        {
            return false;
        }
        return ks.editKeyWord(k);
    }

}
